﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Configuration;
using System.Security.Principal;
using System.Net;

namespace Crosland.Automotriz.Web
{
    public partial class wFrmReportPronosticoImportacion : System.Web.UI.Page
    {
        string idNegocio = ConfigurationManager.AppSettings["Negocio"];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.mostrarReport();
            }
        }

        void mostrarReport()
        {
            try
            {
 int nro_pronostico = Convert.ToInt32(Session["nro_pronostico"].ToString());

            ReportViewer1.ServerReport.ReportServerCredentials = new MyReportServerCredentials();
            ReportViewer1.ProcessingMode = ProcessingMode.Remote;
            ReportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["RsServer"]);
       
            ReportViewer1.ServerReport.ReportPath = "/SAP_Repuestos/rsPronosticoImportancion";
        
            ReportParameter[] parametro = new ReportParameter[1];
            parametro[0] = new ReportParameter();
            parametro[0].Name = "nro_pronostico";
            parametro[0].Values.Add(Convert.ToString(nro_pronostico));
            parametro[0].Visible = false;
     
            ReportViewer1.ServerReport.SetParameters(parametro);
            ReportViewer1.ServerReport.Refresh();
           
            
            
            }
            catch (Exception ex)
            {


                lblmensaje.Text = ex.Message;
                string scriptAprobado = "ShowpopupMsj();";
                ScriptManager.RegisterStartupScript(this, typeof(Page), "carga", scriptAprobado, true);
              
            }



           









        }

        protected void btnSalir_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FrmPronosticoImportacion.aspx?id="+idNegocio);
        }
    }

    //asignando permisos
    [Serializable]
    public sealed class MyReportServerCredentials : IReportServerCredentials
    {
        public WindowsIdentity ImpersonationUser
        {
            get
            {

                return null;
            }
        }

        public ICredentials NetworkCredentials
        {
            get
            {


                // User name
                string userName = ConfigurationManager.AppSettings["RSUser"];

                if (string.IsNullOrEmpty(userName))
                    throw new Exception(
                        "Missing user name from web.config file");

                // Password
                string password =
                    ConfigurationManager.AppSettings["RSPassword"];

                if (string.IsNullOrEmpty(password))
                    throw new Exception(
                        "Missing password from web.config file");

                // Domain
                string domain =
                    ConfigurationManager.AppSettings["RSDomain"];

                if (string.IsNullOrEmpty(domain))
                    throw new Exception(
                        "Missing domain from web.config file");

                return new NetworkCredential(userName, password, domain);
            }
        }

        public bool GetFormsCredentials(out Cookie authCookie,
                    out string userName, out string password,
                    out string authority)
        {
            authCookie = null;
            userName = null;
            password = null;
            authority = null;


            return false;
        }


    }
}